//
//  HZOutsideRoomViewController.m
//  Model
//
//  Created by zeb-apple on 2017/6/2.
//  Copyright © 2017年 YDJH. All rights reserved.
//  室外场景

#import "HZOutsideRoomViewController.h"
#import "OutsideWallView.h"
#import "HZRoom.h"
#import "HZOutsideWall.h"
#import "HZRoomLogic.h"
#import "HZFurniture_Header.h"
#import "FurnitureActionView.h"
#import "RoomSmallMap.h"
#import "HZCommand.h"
#import "HZFurniture.h"
#import "HZRoom.h"
#import "HZBuilding.h"
#import "BuildingData.h"
#import "RoomViewController.h"

@interface HZOutsideRoomViewController ()<CAAnimationDelegate>

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) OutsideWallView *outsideMainWall; //主墙
@property (nonatomic, strong) OutsideWallView *outsideRightWall; //右边的墙
@property (nonatomic, strong) OutsideWallView *outsideWall; //后墙
@property (nonatomic, strong) OutsideWallView *outsideLeftWall; //左边的墙

@property (nonatomic, strong) OutsideWallView *currentWall;


@property (nonatomic, strong) NSMutableArray *walls; //存放四面墙

@property (nonatomic, assign) NSInteger index; //当前墙的位置 0 1 2 3

@property (nonatomic, weak) HZRoom *firstRoom;

@property (nonatomic, strong) HZRoom *room;

@end

@implementation HZOutsideRoomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self initView];
}
- (void)initView {
    
    [self.view addSubview:self.actionView];
    [self.view addSubview:self.roomSmallMap];
    
    [self.view addSubview:self.backgroundView];
    
    
    UISwipeGestureRecognizer *rightSwip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwip:)];
    rightSwip.direction = UISwipeGestureRecognizerDirectionRight;
    [self.backgroundView addGestureRecognizer:rightSwip];
    
    UISwipeGestureRecognizer *leftSwip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwip:)];
    leftSwip.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.backgroundView addGestureRecognizer:leftSwip];
    
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dissmiss)];
    swip.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swip];
    
    
    [self.backgroundView addSubview:self.outsideMainWall];
    [self.backgroundView addSubview:self.outsideRightWall];
    [self.backgroundView addSubview:self.outsideWall];
    [self.backgroundView addSubview:self.outsideLeftWall];
    
    
    [self.walls addObject:self.outsideMainWall]; //主墙 0
    [self.walls addObject:self.outsideRightWall]; //右墙 1
    [self.walls addObject:self.outsideWall]; //后墙 2
    [self.walls addObject:self.outsideLeftWall]; //左墙 3
    
    self.index = 0;
    self.currentWall = self.outsideMainWall;
    
    
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.backgroundView.frame = self.currentWall.frame;
    self.backgroundView.center = self.view.center;
}

- (void)setBuilding:(HZBuilding *)building {
    _building = building;
    self.firstRoom = [HZRoomLogic goBackFiristRoomWithBuildingStructure:(NSMutableArray *)building.buildingStructure];
        
    self.buildingStructure = (NSMutableArray *)_building.buildingStructure;
    
    [self initDataSource];
    
    [self reloadWithRoom:self.firstRoom];
    
}
#pragma mark
#pragma mark getter
- (NSMutableArray *)walls {
    if (!_walls) {
        _walls = [NSMutableArray array];
    }
    return _walls;
}
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] init];
        _backgroundView.backgroundColor = [UIColor whiteColor];
        _backgroundView.center = self.view.center;
    }
    return _backgroundView;
}
- (OutsideWallView *)outsideWall {
    if (!_outsideWall) {
        __weak typeof(self) weakSelf = self;
        _outsideWall = [[OutsideWallView alloc] initWithFrame:CGRectZero furnitureClickBlock:^(HZFurniture *furniture, UIImageView *superView) {
            [weakSelf.actionView reloadCollectionWithFurniture:furniture];
            [weakSelf.actionView show];
        } upBlock:^(HZOutsideWall *outsideWall) {//上爬
            [weakSelf reloadWithRoom:[HZRoomLogic goBackRoomWhenOutsideWithOutsideWall:outsideWall buildingStructure:weakSelf.buildingStructure direction:0]];
        } downBlock:^(HZOutsideWall *outsideWall) {//下爬
            [weakSelf reloadWithRoom:[HZRoomLogic goBackRoomWhenOutsideWithOutsideWall:outsideWall buildingStructure:weakSelf.buildingStructure direction:1]];
        } leftBlock:^(HZOutsideWall *outsideWall) {//左爬
            [weakSelf reloadWithRoom:[HZRoomLogic goBackRoomWhenOutsideWithOutsideWall:outsideWall buildingStructure:weakSelf.buildingStructure direction:2]];
        } rightBlock:^(HZOutsideWall *outsideWall) {//右爬
            [weakSelf reloadWithRoom:[HZRoomLogic goBackRoomWhenOutsideWithOutsideWall:outsideWall buildingStructure:weakSelf.buildingStructure direction:3]];
        }];
        _outsideWall.hidden = YES;
    }
    return _outsideWall;
}
- (OutsideWallView *)outsideLeftWall { //左墙
    if (!_outsideLeftWall) {
        _outsideLeftWall = [[OutsideWallView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-400) furnitureClickBlock:^(HZFurniture *furniture, UIImageView *superView) {
            
        } upBlock:^(HZOutsideWall *outsideWall) {
            
        } downBlock:^(HZOutsideWall *outsideWall) {
            
        } leftBlock:^(HZOutsideWall *outsideWall) {
            
        } rightBlock:^(HZOutsideWall *outsideWall) {
            
        }];
        //  _outsideLeftWall.backgroundColor = [UIColor blueColor];
        _outsideLeftWall.hidden = YES;
        
    }
    return _outsideLeftWall;
}
- (OutsideWallView *)outsideMainWall { //主墙
    if (!_outsideMainWall) {
        __weak typeof(self) weakSelf = self;
        _outsideMainWall = [[OutsideWallView alloc] initWithFrame:CGRectZero furnitureClickBlock:^(HZFurniture *furniture, UIImageView *superView) {
            [weakSelf.actionView reloadCollectionWithFurniture:furniture];
            [weakSelf.actionView show];
        } upBlock:^(HZOutsideWall *outsideWall) {
            
        } downBlock:^(HZOutsideWall *outsideWall) {
            
        } leftBlock:^(HZOutsideWall *outsideWall) {
            
        } rightBlock:^(HZOutsideWall *outsideWall) {
            
        }];
        //   _outsideMainWall.backgroundColor = [UIColor brownColor];
        
    }
    return _outsideMainWall;
    
}
- (OutsideWallView *)outsideRightWall { //右墙
    if (!_outsideRightWall) {
        _outsideRightWall = [[OutsideWallView alloc] initWithFrame:CGRectZero furnitureClickBlock:^(HZFurniture *furniture, UIImageView *superView) {
            
        } upBlock:^(HZOutsideWall *outsideWall) {
            
        } downBlock:^(HZOutsideWall *outsideWall) {
            
        } leftBlock:^(HZOutsideWall *outsideWall) {
            
        } rightBlock:^(HZOutsideWall *outsideWall) {
            
        }];
        //   _outsideRightWall.backgroundColor = [UIColor greenColor];
        _outsideRightWall.hidden = YES;
        
    }
    return _outsideRightWall;
    
}
#pragma mark
#pragma mark 手势处理
- (void)rightSwip:(UISwipeGestureRecognizer *)recognizer {
    //判断是不是一间房
    if (self.room.isFirstSurfaceRoom) {
        OutsideWallView *from = self.walls[self.index];
        self.index = (self.index+1) <= 3 ? (self.index+1) :0;
        OutsideWallView *to = self.walls[self.index];
        
        [self animationViewFrom:from to:to direction:kCATransitionFromLeft];
        
    }
    
}
- (void)leftSwip:(UISwipeGestureRecognizer *)recognizer {
    
    if (self.room.isFirstSurfaceRoom) {
        OutsideWallView *from = self.walls[self.index];
        self.index = (self.index-1) < 0 ? 3 :(self.index-1);
        OutsideWallView *to = self.walls[self.index];
        
        [self animationViewFrom:from to:to direction:kCATransitionFromRight];
    }
    
}

- (void)animationViewFrom:(OutsideWallView *)from to:(OutsideWallView *)to direction:(NSString *)direction{
    NSLog(@"----:%ld",self.index);
    self.view.userInteractionEnabled = NO;
    CATransition *animation = [CATransition animation];
    animation.duration = 0.8;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    animation.delegate = self;
    animation.type = @"cube";
    animation.subtype = direction;
    
    [[self.backgroundView layer] addAnimation:animation forKey:@"cube"];
    
    from.hidden = YES;
    to.hidden = NO;
    self.currentWall = to;//设置当前停留view
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    self.view.userInteractionEnabled = YES;
}
#pragma mark
#pragma mark action
//指令集点击事件
- (void)actionWithFurniture:(HZFurniture *)furniture command:(HZCommand *)command {
    
    [super actionWithFurniture:furniture command:command];
    
    if (self.index == Main_Wall) {
       [self reloadWall:self.room.outsideWall3];
    }else if (self.index == Back_Wall) {
        [self reloadWall:self.room.outsideWall1];
    }
    SEL selector = [HZRoomLogic goBackSelectFromHZOutsideRoomViewControllerActionWithFurniture:furniture command:command];
    if ([self respondsToSelector:selector]) {
        SuppressPerformSelectorLeakWarning([self performSelector:selector withObject:furniture withObject:command]);
    }
    
}
// 进房间内部
- (void)gotoInsideRoom {
    RoomViewController *roomViewController = [[RoomViewController alloc] init];
    roomViewController.buildingStructure = (NSMutableArray *)self.building.buildingStructure;
    [roomViewController reloadWithRoom:self.room];
    roomViewController.modalTransitionStyle = 2;
    [self presentViewController:roomViewController animated:YES completion:nil];
    __weak typeof(self) weakSelf = self;
    roomViewController.dissmissBlock = ^(RoomViewController *controller, NSInteger wall, HZRoom *room) {
        OutsideWallView *from = weakSelf.walls[weakSelf.index];
        weakSelf.index = wall;
        OutsideWallView *to = weakSelf.walls[weakSelf.index];
        from.hidden = YES;
        to.hidden = NO;
        weakSelf.currentWall = to;//设置当前停留view
        [weakSelf reloadWithRoom:room];
    };
}

#pragma mark
#pragma mark action
// 进房间
- (void)enterRoomWithWindow:(HZFurniture *)furniture command:(HZCommand *)command {
    
    [self gotoInsideRoom];
}
- (void)enterRoomWithDoor:(HZFurniture *)furniture command:(HZCommand *)command {
    
    [self gotoInsideRoom];
}
// 上楼
- (void)climbUpWithLabber:(HZFurniture *)furniture command:(HZCommand *)command {
    if ([furniture.furnitureState isEqualToString:destory_state]) { //被破坏
        
    }else {
        //当前房间的位置
        NSIndexPath *indexPath = [HZRoomLogic goBackBuildingCurrentIndex];
        
        HZRoom *room = self.buildingStructure[indexPath.section][indexPath.row];
        
        [self reloadWithRoom:[HZRoomLogic goBackRoomWhenOutsideWithOutsideWall:room.outsideWall1 buildingStructure:self.buildingStructure direction:0]];
    }
}
// 下楼
- (void)climbDownWithLabber:(HZFurniture *)furniture command:(HZCommand *)command {
    if ([furniture.furnitureState isEqualToString:destory_state]) { //被破坏
        
    }else {
        //当前房间的位置
        NSIndexPath *indexPath = [HZRoomLogic goBackBuildingCurrentIndex];
        
        HZRoom *room = self.buildingStructure[indexPath.section][indexPath.row];
        
        [self reloadWithRoom:[HZRoomLogic goBackRoomWhenOutsideWithOutsideWall:room.outsideWall1 buildingStructure:self.buildingStructure direction:1]];
    }
}
- (void)dissmiss {
    if (self.room.isFirstSurfaceRoom) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
#pragma mark
#pragma mark reloadwall
//根据房间刷新数据
- (void)reloadWithRoom:(HZRoom *)room {
    
    if (room) {
        //当前房间的
        self.room = room;
        //更新当前房间位置
        [HZRoomLogic updataBuildingCurrentIndex:room.position];
        
        if (self.index == Main_Wall) { //主墙
            HZOutsideWall *wall = room.outsideWall3;
            UIImage *image = [UIImage imageNamed:wall.wallPicture];
            
            self.outsideMainWall.image = image;
            self.outsideMainWall.frame = CGRectMake(0, 0, image.size.width, image.size.height);
            // self.outsideWall.center = self.view.center;
            
            [self reloadWall:wall];
        }else if (self.index == Back_Wall) { //后墙
            HZOutsideWall *wall = room.outsideWall1;
            UIImage *image = [UIImage imageNamed:wall.wallPicture];
            
            self.outsideWall.image = image;
            self.outsideWall.frame = CGRectMake(0, 0, image.size.width, image.size.height);
            // self.outsideWall.center = self.view.center;
            
            [self reloadWall:wall];
        }
        
        
        room.roomState = RoomState_Coming;
        
        //更新小地图
        [self.roomSmallMap reloadData:(NSMutableArray *)[[self.buildingStructure reverseObjectEnumerator] allObjects]];
        
    }
    
}
#pragma mark
#pragma mark initdatasource
- (void)initDataSource {
    
    [self.outsideWall reloadWithOutsideWall:self.firstRoom.outsideWall1];
    [self.outsideLeftWall reloadWithOutsideWall:self.firstRoom.outsideWall2];
    [self.outsideMainWall reloadWithOutsideWall:self.firstRoom.outsideWall3];
    [self.outsideRightWall reloadWithOutsideWall:self.firstRoom.outsideWall4];
    
    self.outsideMainWall.image = [UIImage imageNamed:self.building.buildingData.outsideMainWallPicture];
    self.outsideMainWall.frame = CGRectMake(0, 0, self.outsideMainWall.image.size.width, self.outsideMainWall.image.size.height);
    
    self.outsideLeftWall.image = [UIImage imageNamed:self.building.buildingData.outsideLeftWallPicture];
    self.outsideLeftWall.frame = CGRectMake(0, 0, self.outsideLeftWall.image.size.width, self.outsideLeftWall.image.size.height);
    
    self.outsideRightWall.image = [UIImage imageNamed:self.building.buildingData.outsideRightWallPicture];
    self.outsideRightWall.frame = CGRectMake(0, 0, self.outsideRightWall.image.size.width, self.outsideRightWall.image.size.height);
    
    self.outsideWall.image = [UIImage imageNamed:self.building.buildingData.outsideBackWallPicture];
    self.outsideWall.frame = CGRectMake(0, 0, self.outsideWall.image.size.width, self.outsideWall.image.size.height);
    
}

- (void)reloadWall:(HZOutsideWall *)wall {
    [self.currentWall reloadWithOutsideWall:wall];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
